<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="车牌号" prop="carId">
        <el-select
          v-model="queryParams.carId"
          filterable
          placeholder="请选择电动车车牌号"
          clearable
          :style="{ width: '100%' }"
        >
          <el-option
            v-for="(item, index) in carOptions"
            :key="index"
            :label="item.label"
            :value="item.value"
            :disabled="item.disabled"
          ></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="电动车id" prop="carId">
        <el-input
          v-model="queryParams.carId"
          placeholder="请输入电动车id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="充电桩id" prop="stationId">
        <el-input
          v-model="queryParams.stationId"
          placeholder="请输入充电桩id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="充电桩" prop="stationId">
        <el-select
          v-model="queryParams.stationId"
          filterable
          placeholder="请选择充电桩"
          clearable
          :style="{ width: '100%' }"
        >
          <el-option
            v-for="(item, index) in stationOptions"
            :key="index"
            :label="item.label"
            :value="item.value"
            :disabled="item.disabled"
          ></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="开始充电时间" prop="chargeStart">
        <el-date-picker
          clearable
          v-model="queryParams.chargeStart"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始充电时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束充电时间" prop="chargeEnd">
        <el-date-picker
          clearable
          v-model="queryParams.chargeEnd"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结束充电时间"
        >
        </el-date-picker>
      </el-form-item> -->

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row style="margin: 10px" :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              
              @confirm="handleDelBatch"
          >
            <el-button type="danger" :disabled="multiple" icon="el-icon-delete"  plain slot="reference">批量删除</el-button>
          </el-popconfirm>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>

    <el-table
      v-loading="loading"
      :data="recordList"
      @selection-change="handleSelectionChange"
      size="small"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="记录id" align="center" prop="recordId" />
      <el-table-column label="车牌号" align="center" prop="carCode" />
      <el-table-column label="充电区域" align="center" prop="areaName" />
      <el-table-column label="充电桩" align="center" prop="stationCode" />
      <el-table-column label="开始充电时间" align="center" prop="chargeStart" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.chargeStart) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束充电时间" align="center" prop="chargeEnd" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.chargeEnd) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="充电时长" align="center" width="150" :formatter="formatReportType">
        <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.chargeStart) }}</span>
        </template> -->
      </el-table-column>
      <el-table-column label="充电花费" align="center" prop="money" />
      <!-- <el-table-column label="备注" align="center" prop="recordRemark" /> -->
      <el-table-column
        label="操作"
        width="200px"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="success"
            plain
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="handleDelete(scope.row)"
          >
            <el-button type="danger" icon="el-icon-delete"  plain slot="reference">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改充电记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="车牌号" prop="carId">
          <el-select
            v-model="form.carId"
            filterable
            placeholder="请选择电动车车牌号"
            clearable
            :style="{ width: '100%' }"
          >
            <el-option
              v-for="(item, index) in carOptions"
              :key="index"
              :label="item.label"
              :value="item.value"
              :disabled="item.disabled"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="充电桩" prop="stationId">
          <el-select
            v-model="form.stationId"
            filterable
            placeholder="请选择充电桩"
            clearable
            :style="{ width: '100%' }"
          >
            <el-option
              v-for="(item, index) in stationOptions"
              :key="index"
              :label="item.label"
              :value="item.value"
              :disabled="item.disabled"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始充电时间" prop="chargeStart">
          <el-date-picker
            clearable
            v-model="form.chargeStart"
            type="datetime"
            placeholder="请选择开始充电时间"
            value-format="yyyy-MM-dd HH:mm:ss"
            default-time="12:00:00"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束充电时间" prop="chargeEnd">
          <el-date-picker
            clearable
            v-model="form.chargeEnd"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择结束充电时间"
            default-time="12:00:00"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="充电花费" prop="money">
          <el-input v-model="form.money" placeholder="请输入充电花费" />
        </el-form-item>
        <el-form-item label="备注" prop="recordRemark">
          <el-input v-model="form.recordRemark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { serverIp } from "../../public/config";
import { listRecord, getRecord, delRecord, addRecord, updateRecord } from "@/api/record";
import { carTotal } from "@/api/car";
import { stationTotal } from "@/api/station";
const dayjs = require("dayjs");
export default {
  name: "Record",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 充电记录表格数据
      recordList: [],
      carOptions: [],
      stationOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        carId: null,
        stationId: null,
        chargeStart: null,
        chargeEnd: null,
        money: null,
        recordRemark: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        carId: [{ required: true, message: "电动车id不能为空", trigger: "blur" }],
        stationId: [{ required: true, message: "充电桩id不能为空", trigger: "blur" }],
        chargeStart: [
          { required: true, message: "开始充电时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getCarList();
    this.getStationList();
  },
  methods: {
    formatReportType(row) {
      let value = "";
      var lefttime = dayjs(row.chargeEnd).valueOf() - dayjs(row.chargeStart).valueOf();
      if (lefttime+"" == 'NaN') {
        return value;
       
      } else {
        var leftd = Math.floor(lefttime / (1000 * 60 * 60 * 24)); //计算天数
        var lefth = Math.floor((lefttime / (1000 * 60 * 60)) % 24); //计算小时数
        var leftm = Math.floor((lefttime / (1000 * 60)) % 60); //计算分钟数
        var lefts = Math.floor((lefttime / 1000) % 60); //计算秒数
        return leftd + "天" + lefth + "小时" + leftm + "分" + lefts + "秒";
      }

      // value = new Date().getTime()
      // return value;
    },
    /** 查询电动车列表 */
    getCarList() {
      carTotal().then((response) => {
        const list = response.data.map((e) => {
          const obj = {
            value: e.carId,
            label: e.carCode,
          };
          return obj;
        });
        this.carOptions = list;
      });
    },
    /** 查询电动车列表 */
    getStationList() {
      stationTotal().then((response) => {
        const list = response.data.map((e) => {
          const obj = {
            value: e.stationId,
            label: e.stationCode,
          };
          return obj;
        });
        this.stationOptions = list;
      });
    },

    /** 查询充电记录列表 */
    getList() {
      this.loading = true;
      listRecord(this.queryParams).then((response) => {
        this.recordList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        recordId: null,
        carId: null,
        stationId: null,
        chargeStart: null,
        chargeEnd: null,
        money: null,
        recordRemark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      // this.queryParams = {
      //     pageNum: 1,
      //     pageSize: 10,
      //     carId: null,
      //     stationId: null,
      //     chargeStart: null,
      //     chargeEnd: null,
      //     money: null,
      //     recordRemark: null,
      //   },
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.recordId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加充电记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const recordId = row.recordId || this.ids;
      getRecord(recordId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改充电记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.recordId != null) {
            updateRecord(this.form).then((response) => {
              this.$message.success("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecord(this.form).then((response) => {
              this.$message.success("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
   /** 删除按钮操作 */
   handleDelete(row) {
      const recordIds = row.recordId;
      delRecord(recordIds)
        .then(() => {
          this.getList();
          this.$message.success("删除成功");
        })
    },
    handleDelBatch(){
      const recordIds = this.ids;
      delRecord(recordIds)
        .then(() => {
          this.getList();
          this.$message.success("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      window.open(`http://${serverIp}:9090/record/export`);
      // this.download(
      //   "record/export",
      //   {
      //     ...this.queryParams,
      //   },
      //   `record_${new Date().getTime()}.xlsx`
      // );
    },
  },
};
</script>
